package com.wondersgroup.framework.widget.taglib.form;

import com.wondersgroup.framework.widget.annotations.WidgetTag;
import com.wondersgroup.framework.widget.annotations.WidgetTagAttribute;
import com.wondersgroup.framework.widget.library.extjs.ExtComponent;
import com.wondersgroup.framework.widget.library.extjs.form.ExtCheckbox;
import com.wondersgroup.framework.widget.library.extjs.form.ExtField;
import com.wondersgroup.framework.widget.library.extjs.form.ExtRadio;
import com.wondersgroup.framework.widget.library.html.Div;
import com.wondersgroup.framework.widget.library.html.HtmlComponent;
import com.wondersgroup.framework.widget.taglib.form.util.FormUtils;
import com.wondersgroup.framework.widget.util.OutputUtils;
import com.wondersgroup.framework.widget.util.TagUtils;
import com.wondersgroup.framework.widget.validation.annotation.TagValidate;
import com.wondersgroup.framework.widget.validation.annotation.TypeValidate;
import com.wondersgroup.framework.widget.validation.annotation.ValueValidate;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import org.springframework.beans.BeanUtils;

@WidgetTag(name = "checkbox", tldTagClass = "com.wondersgroup.framework.widget.taglib.form.CheckboxTag", description = "选择框控件，通过设置type来进行判断是复选框还是单选框")
@TagValidate
public class CheckboxTag extends RequiredTag {

	@TypeValidate
	private boolean checked;
	private String text;
	private String inputValue;
	private String width = "0";

	@ValueValidate({ "radio", "checkbox" })
	private String type = "checkbox";
	private String outputContent;
	private String onCheck;
	private String tooltip;

	public String getOnCheck() {
		return this.onCheck;
	}

	@WidgetTagAttribute(description = "选中后的事件")
	public void setOnCheck(String paramString) {
		this.onCheck = paramString;
	}

	public boolean isChecked() {
		return this.checked;
	}

	@WidgetTagAttribute(description = "复选框是否被选中，默认为false")
	public void setChecked(boolean paramBoolean) {
		this.checked = paramBoolean;
	}

	public String getText() {
		return this.text;
	}

	@WidgetTagAttribute(description = "复选框旁边显示的文本", required = true)
	public void setText(String paramString) {
		this.text = paramString;
	}

	public String getInputValue() {
		return this.inputValue;
	}

	@WidgetTagAttribute(description = "指把一个值设置到指定的键，形成键值对形式的数据，用于数据交互的实际值")
	public void setInputValue(String paramString) {
		this.inputValue = paramString;
	}

	public String getType() {
		return this.type;
	}

	@WidgetTagAttribute(description = "渲染类型---包括：radio(单选)、checkbox(复选)，默认为checkbox(复选)", defaultValue = "checkbox")
	public void setType(String paramString) {
		this.type = paramString;
	}

	public String getWidth() {
		return this.width;
	}

	@WidgetTagAttribute(description = "组件宽度，默认为0")
	public void setWidth(String paramInt) {
		this.width = paramInt;
	}

	public String getTooltip() {
		return this.tooltip;
	}

	@WidgetTagAttribute(description = "复选框提示注释")
	public void setTooltip(String paramString) {
		this.tooltip = paramString;
	}

	public String getOutputContent() {
		return this.outputContent;
	}

	public void setOutputContent(String paramString) {
		this.outputContent = paramString;
	}

	public void processStartTagContent() throws JspException {
		Div localDiv = new Div(getId() + "_div");
		Object localObject1 = null;
		Object localObject2 = null;
		if ("checkbox".equals(getType())) {
			localObject2 = new ExtCheckbox(getId());
			BeanUtils.copyProperties(this, localObject2);
			((ExtCheckbox) localObject2).setFormItems(getFormItems());
			if ((getToolBarArray() == null) && (getStatusBarArray() == null)) {
				((ExtCheckbox) localObject2).setRenderTo(localDiv.getId());
			}	
			localObject1 = localObject2;
		} else if ("radio".equals(getType())) {
			localObject2 = new ExtRadio(getId());
			BeanUtils.copyProperties(this, localObject2);
			((ExtRadio) localObject2).setFormItems(getFormItems());
			if ((getToolBarArray() == null) && (getStatusBarArray() == null)) {
				((ExtRadio) localObject2).setRenderTo(localDiv.getId());
			}
			localObject1 = localObject2;
		}
		localObject2 = new StringBuffer();
		((StringBuffer) localObject2).append(OutputUtils.writeHTML(new HtmlComponent[] { localDiv }));
		((StringBuffer) localObject2).append(OutputUtils.ExtOnReady(new ExtComponent[] { (ExtField) localObject1 }));
		FormUtils.addFieldsToForm(this.pageContext, (ExtField) localObject1);
		if (getParent() instanceof CheckboxGroupTag) {
			((ExtField) localObject1).setProperty(((CheckboxGroupTag) getParent()).getProperty());
			setOutputContent(((ExtCheckbox) localObject1).getJS2());
		} else {
			TagUtils.getInstance().write(this.pageContext, ((StringBuffer) localObject2).toString());
		}
	}

	public void processEndTagContent() throws JspException {
		if (getParent() instanceof CheckboxGroupTag) {
			List localList = (List) this.pageContext.getAttribute("checkItems");
			CheckboxTag localCheckboxTag = new CheckboxTag();
			BeanUtils.copyProperties(this, localCheckboxTag);
			localList.add(localCheckboxTag);
			this.pageContext.setAttribute("checkItems", localList);
		}
	}

	@WidgetTagAttribute(description = "表单交互时的元素名称", required = false)
	public void setProperty(String paramString) {
		super.setProperty(paramString);
	}
}